import type { MockMethod } from 'vite-plugin-mock';

const companyList = []
const deptList = []

// noinspection JSUnusedGlobalSymbols
export default [
    {
        url: '/dev-api/wisdom-ab/company',
        method: 'post',
        response: (config) => {
            if(config.body.id !== undefined) {
                // 修改
                for(let i = 0; i < companyList.length; i++) {
                    if(companyList[i].id === config.body.id) {
                        companyList[i] = config.body
                    }
                }
            }else {
                //新增
                const result = companyList.filter(company => config.body.companyCode === company.companyCode)
                if(result.length > 0) {
                    return {
                        "msg": "公司编码"+result[0].companyCode+"已存在！",
                        "code": -1,
                        "ds":""
                    }
                }
                config.body.id = "1111"
                config.body.createTime = '@now'
                companyList.push(config.body)
            }
            return {
                "msg": "保存成功",
                "code": 0,
                "ds":"保存成功"
            }
        }
    },
    {
        url: '/dev-api/wisdom-ab/company',
        method: 'get',
        response: (config) => {
            const companyId = config.query.companyId
            for(let i = 0; i < companyList.length; i++) {
                if(companyList[i].id === companyId) {
                    return {
                        "msg": "查询公司详情成功",
                        "code": 0,
                        "ds": companyList[i]
                    }
                }
            }
            return {
                "msg": "查询公司详情失败",
                "code": -1,
                "ds": {}
            }
        }
    },
    {
        url: '/dev-api/wisdom-ab/company/list',
        method: 'get',
        response: (config) => {
            let result = companyList
            if(config.query.companyCode !== undefined) {
                result = result.filter(company => {
                    console.log(company.companyCode)
                    return company.companyCode.includes(config.query.companyCode)
                })
            }
            if(config.query.companyName !== undefined) {
                result = result.filter(company => company.companyName.includes(config.query.companyName))
            }
            return {
                "msg": "查询公司列表成功",
                "code": 0,
                "ds": {
                    "pageNumber":config.query.pageNum,
                    "pageSize":config.query.pageSize,
                    "totalPages":result.length % config.query.pageSize,
                    "totalElements":result.length,
                    "content":result
                }
            }
        }
    },
    {
        url: /\/dev-api\/wisdom-ab\/company\//,
        method: 'delete',
        response: (config) => {
            console.log(config.url)
            let paths = config.url.split("/")
            let companyId = paths[4]
            for(let i = 0; i < companyList.length; i++) {
                if(companyList[i].id === companyId) {
                    companyList.splice(i,1)
                    break;
                }
            }
            return {
                "msg": "删除公司成功",
                "code": 0,
                "ds":""
            }
        }
    },
    {
        url: '/dev-api/wisdom-ab/department/list',
        method: 'get',
        response: (config) => {
            let _companyList = []
            if(config.query.companyId !== undefined) {
                _companyList = companyList.filter(company => company.id === config.query.companyId)
            }
            let _deptList = deptList.slice();
            if(config.query.deptName !== undefined) {
                _deptList = _deptList.filter(dept => dept.deptName.includes(config.query.deptName))
            }
            if(_companyList.length > 0) {
                _deptList.push({
                    "id":_companyList[0].id,
                    "parentId":"0",
                    "deptName":_companyList[0].companyName,
                    "orderNum":0,
                    "status":0,
                    "children":[]
                })
            }
            return {
                "msg": "查询部门成功",
                "code": 0,
                "ds": _deptList
            }
        }
    },
    {
        url: '/dev-api/wisdom-ab/department',
        method: 'post',
        response: (config) => {
            console.log(config.body)
            if(config.body.id !== undefined) {
                // 修改
                for(let i = 0; i < deptList.length; i++) {
                    if(deptList[i].id === config.body.id) {
                        deptList[i] = config.body
                    }
                }
            }else {
                //新增
                config.body.id = "1122333"
                config.body.deptCode = deptList.length+1
                config.body.createTime = '@now'
                deptList.push(config.body)
            }
            return {
                "msg": "保存成功",
                "code": 0,
                "ds":"保存成功"
            }
        }
    },
    {
        url: '/dev-api/wisdom-ab/department',
        method: 'get',
        response: (config) => {
            const deptId = config.query.deptId
            for(let i = 0; i < deptList.length; i++) {
                if(deptList[i].id === deptId) {
                    return {
                        "msg": "查询部门详情成功",
                        "code": 0,
                        "ds": deptList[i]
                    }
                }
            }
            return {
                "msg": "查询部门详情失败",
                "code": -1,
                "ds": {}
            }
        }
    },
] as MockMethod[];
